package com.example.utils.responseHeader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

public class ResponseHeader {

    /**
     * 发送响应流方法
     */
    public static void setResponseHeader(HttpServletRequest request, HttpServletResponse response, String fileName) {
        try {
            response.setContentType("multipart/form-data");
            response.addHeader("Content-type", "appllication/octet-stream");
            String userAgent = request.getHeader("User-Agent");//获取浏览器名（IE/Chome/firefox）
            if(userAgent.contains("MSIE")||userAgent.contains("Trident")) {//针对IE或IE为内核的浏览器
                try {
                    fileName=java.net.URLEncoder.encode(fileName,"UTF-8");//文件名内容
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    fileName=new String(fileName.getBytes("UTF-8"),"ISO-8859-1");//谷歌控制版本
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("content-type", "text/html;charset=UTF-8");
            response.addHeader("Cache-Control", "no-cache");
            response.addHeader("Pargam", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setCharacterEncoding("utf-8");//内容字符

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
